﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using ConfigGen.ConsoleApp.Core.CommandLineOptionCore;
using ConfigGen.Core;

namespace ConfigGen.ConsoleApp.Core.CommandLineOptions
{
    /// <summary>
    /// Console argument handler to handle the "pretty print line length" command line option. This processor is responsible for setting the
    /// <see cref="Preferences.PrettyPrintLineLength"/> property on the settings object.
    /// </summary>
    public class PrettyPrintLineLength : ICommandLineOption<ConsoleAppPreferences>
    {
        private int _lineLength;

        #region ICommandLineOption<ConsoleAppPreferences> Members

        /// <summary>
        /// Gets the short name of the command line argument, without the leading hyphen; for this processor: "ppll".
        /// </summary>
        /// <value>"ppll"</value>
        public string ShortName
        {
            get { return "ppll"; }
        }

        /// <summary>
        /// Gets the long name of the command line argument, without the leading hyphens; for this processor: "pretty-print-line-length"
        /// </summary>
        /// <value>"pretty-print"</value>
        public string LongName
        {
            get { return "pretty-print-line-length"; }
        }

        /// <summary>
        /// Gets a help string containing the args usage for this processor: "&lt;line-length&gt;"
        /// </summary>
        /// <value>"&lt;line-length&gt;"</value>
        public string ArgsUsage
        {
            get { return "<line-length>"; }
        }

        /// <summary>
        /// Gets help text for this command line option.
        /// </summary>
        /// <value>Help text</value>
        public string HelpText
        {
            get
            {
                var prettyPrint = new PrettyPrint();
                return
                    string.Format(
                        "sets the maximum line length while pretty printing. This setting must be used in conjunction with the pretty print option, -{0} / --{1}. NOTE: This setting has been deprecated - pretty print preferences should now be set via the configuration template itself (in the preferences section). However, this setting will override the setting in the configuration template.",
                        prettyPrint.ShortName,
                        prettyPrint.LongName);
            }
        }

        /// <summary>
        /// Applies the option represented by this instance to the supplied settings instance.
        /// </summary>
        /// <param name="settings">The settings object.</param>
        public void ApplyOptionToSettings(ConsoleAppPreferences settings)
        {
            settings.PrettyPrintLineLength = _lineLength;
        }

        /// <summary>
        /// Processes the argument queue by consuming (dequeuing) any arguments used by this instance. For this type of processor,
        /// a single argument, the line length, is consumed
        /// </summary>
        /// <param name="args">Args queue from which to consume args</param>
        public void ConsumeArgumentsFromQueue(Queue<string> args)
        {
            if (args.Count < 1)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "filename switch [-{0} or --{1}] expects a filename argument",
                                        ShortName,
                                        LongName);
                throw new ArgumentException(msg);
            }

            string arg = args.Dequeue();
            int lineLength;

            if (int.TryParse(arg, out lineLength))
            {
                _lineLength = lineLength;
            }
            else
            {
                throw new ArgumentException("The pretty print line length option expects an integer argument, but got: " + arg);
            }
        }

        #endregion
    }
}

